# -*- codeing = utf-8 -*-
# @Time : 2024/8/5 12:38
# @Author:北希
# @File :word_cloud.py
# @Software: PyCharm
import jieba
from wordcloud import WordCloud
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
from pymysql import *
import json



def get_img(field,targetImageSrc,resImageSrc):
    con = connect (host='localhost',user='root',password='ren',database='cardata',port=3306,charset = 'utf8mb4')

    cusor = con.cursor()
    sql = f"select {field} from carinfo"
    cusor.execute(sql)
    data = cusor.fetchall()

    text = ''
    for i in data:
        if i[0] != '':
            tagArr = i
            for j in tagArr:
                text += j
    cusor.close()
    con.close()
    data_cut = jieba.cut(text,cut_all = False)
    string = ' '.join(data_cut) #一定要按空格

    #图片
    img = Image.open(targetImageSrc)
    img_arr = np.array(img)
    wc = WordCloud(
        font_path = 'STHUPO.TTF',
        mask = img_arr,
        background_color='#04122c',
        max_font_size=200,
        min_font_size=1
    )
    wc.generate_from_text(string)
    #绘制图片
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off')
    plt.savefig(resImageSrc,dpi=800,bbox_inches = 'tight',pad_inches = -0.1) #去除白边

get_img('manufacturer','./big-screen-vue-datav-master/public/车.png','./big-screen-vue-datav-master/public/car_cloud.png')
